import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.base import MIMEBase
from email_config import sectpass
from email import encoders

import sys
import tarfile
import time
import paramiko

def send_email(sender_email,sender_pass,rec_email,subject,message,attachment_paths):
    #创建邮件对象
    msg = MIMEMultipart()
    msg['Subject'] = subject
    msg['From'] = sender_email
    msg['To'] = rec_email
    #添加邮件正文
    #msg.attach(MIMEText(message,'plain','utf-8'))
    msg.attach(MIMEText(message, 'plain'))
    #添加邮件附件
    for res in attachment_paths:
        if res.endswith('.jpg'):
            with open(res,'rb') as f:
                part = MIMEImage(f.read())
                part.add_header('Content-Disposition', 'attachment', filename=res)
                msg.attach(part)
        elif res.endswith(".xlsx"):
            with open(res,'rb') as f:
                part = MIMEBase('application', 'octet-stream')
                part.set_payload(f.read())
                encoders.encode_base64(part)
                part.add_header('Content-Disposition','attachment',filename=res)
                msg.attach(part)
        elif res.endswith(".txt"):
            with open(res,'rb') as f:
                part = MIMEBase('Content-Disposition', 'octet-stream')
                part.set_payload(f.read())
                encoders.encode_base64(part)
                part.add_header('Content-Disposition','attachment',filename=res)
                msg.attach(part)
        elif res.endswith(".zip"):
            with open(res,'rb') as f:
                part = MIMEBase('application', 'octet-stream')
                part.set_payload(f.read())
                encoders.encode_base64(part)
                part.add_header('Content-Disposition','attachment',filename=res)
                msg.attach(part)
    #连接邮件smtp服务器发送邮件
    # with smtplib.SMTP_SSL('smtp.qq.com',465) as smtp:
    #     smtp.login(sender_email,sender_pass)
    #     smtp.send_message(msg)
    #     print("邮件发送完毕")
    smtp = smtplib.SMTP('smtp.qq.com', 587)
    smtp.starttls()  # 手动开启 TLS 加密
    smtp.login(sender_email, sender_pass)
    smtp.send_message(msg)
    smtp.quit()
    print("邮件发送完毕")

def  pack():
    tar =  tarfile.open("a.tar.gz",mode="w:gz")
    tar.add("report")
def  upload(filename):
    remote_dir = r"/usr/local/nginx/html/allure"
    username  = "root"
    password = "123456"
    hostname = "192.168.45.132"
    client = paramiko.SSHClient()

    # 设置自动添加到系统的主机列表中
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # 连接服务器
    client.connect(hostname=hostname,
                   username=username,
                   password=password,
                   port=22,
                   timeout=10
                )

    # 删除远程仓库的a.tar.gz报告文件
    # client.exec_command(f"rm -rf  {remote_dir}/a.tar.gz")
    client.exec_command(f"rm -rf  {remote_dir}/*")
    # 上传文件包数据
    sftp = client.open_sftp()

    #  上传
    sftp.put("a.tar.gz",remote_dir+"/a.tar.gz")

    # 等待时间
    time.sleep(3)
    print("上传成功！！！！")
    # 解压
    client.exec_command(f"cd  {remote_dir}")
    time.sleep(1)
    client.exec_command(f"tar -xvf   {remote_dir}/a.tar.gz -C  {remote_dir}")
    time.sleep(2)

    sftp.close()
    client.close()

def main():
    pack()
    sender_email = "1398853301@qq.com"
    shouquanma = sectpass
    to_email = "handong97@163.com"
    sub_msg = '这是一个测试邮件'
    txt = 'hello'
    attach_paths = [

        'report.zip'
    ]
    send_email(sender_email,shouquanma,to_email,sub_msg,txt,attach_paths)



if __name__ == '__main__':
    main()
    pack()
    upload("a.tar.gz")


# if __name__ == '__main__':
#     main()




